import pygame, sys, math, random

pygame.init()

from Crab import Crab
from Player import Player
from Bullet import Bullet

clock = pygame.time.Clock()

width = 1024
height = 768
size = width, height
   
screen = pygame.display.set_mode(size)

def reset():
    player = Player(["rsc/player/player.png"], [0,0], [100,100], [width/2,height/2])
    playerhealth = 3
    level = 1
    for crab in crabs:
        crab.living = False

bgImage = pygame.image.load("rsc/bg/startbg.png")
bgImage = pygame.transform.scale(bgImage, size)
bgRect = bgImage.get_rect()

bgColor = r,g,b = 0,0,0

player = Player(["rsc/player/player.png"], [14,14], [100,100], [width/2,height/2])

playerhealth = 3
crabpoints = 0
bsize = random.randint(25, 150)
crabs = [Crab("rsc/enemy/crab.png", 
              [random.randint(-5,5), random.randint(-5,5)], 
              [bsize, bsize], 
              [random.randint(75, width-75), random.randint(75, height-75)])]
              
bullets = []
bulletTimerMax = 20
bulletTimer = bulletTimerMax
bulletCoolDown = False

mode = "start"
while True:
    
    bgImage = pygame.image.load("rsc/bg/startbg.png")
    bgImage = pygame.transform.scale(bgImage, size)
    while mode == "start":
        reset()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    sys.exit()
                
           
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    mode = "game"
                    
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_s:
                    mode = "shop"
            
        
        screen.blit(bgImage, bgRect)
        pygame.display.flip()
        clock.tick(60)
    
    bgImage = pygame.image.load("rsc/bg/beach.png")
    bgImage = pygame.transform.scale(bgImage, size)    
 
    
    while mode == "shop":
        bgImage = pygame.image.load("rsc/bg/shop.png")
        bgImage = pygame.transform.scale(bgImage, size) 
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_p:
                    mode == "game"
                if event.key == pygame.K_ESCAPE:
                    sys.exit()
        
    level = 0
    
    while mode =="game" and player.living:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_p:
                    mode == "shop"
                if event.key == pygame.K_RIGHT:
                    player.direction("right")
                if event.key == pygame.K_LEFT:
                    player.direction("left")
                if event.key == pygame.K_UP:
                    player.direction("up")
                if event.key == pygame.K_DOWN:
                    player.direction("down")
                if event.key == pygame.K_ESCAPE:
                    sys.exit()
                if event.key == pygame.K_SPACE:
                    if not bulletCoolDown:
                        bullets += [Bullet("up", player.rect.center)]
                        bullets += [Bullet("down", player.rect.center)]
                        bulletCoolDown = True
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_RIGHT:
                    player.direction("stop right")
                if event.key == pygame.K_LEFT:
                    player.direction("stop left")
                if event.key == pygame.K_UP:
                    player.direction("stop up")
                if event.key == pygame.K_DOWN:
                    player.direction("stop down")
                    

        if bulletCoolDown:
            if bulletTimer > 0:
                bulletTimer -= 1
            else:
                bulletTimer = bulletTimerMax
                bulletCoolDown = False
        
        for crab in crabs:
            crab.update()
        for bullet in bullets:
            bullet.update()
        player.update() 
               
        
        for crab in crabs:
            crab.collideWall(width, height)
        for bullet in bullets:
            bullet.collideWall(width, height)
        player.collideWall(width, height)
        
        if len(crabs) > 1:
            for first in range(len(crabs)-1):
                for second in range(first+1,len(crabs)):
                    crabs[first].collideCrab(crabs[second])
        
        for bullet in bullets:
            for crab in crabs:
                bullet.collideCrab(crab)
        
        for crab in crabs:
            player.collideCrab(crab)
                
            
            
        for crab in crabs:
            if not crab.living:
                crabs.remove(crab)
        
        for bullet in bullets:
            if not bullet.living:
                bullets.remove(bullet)          
        
        if len(crabs) == 0:
            level += 1
            for i in range(level):
                print i
                bsize = random.randint(25, 150)
                hittingPlayer = True
                while hittingPlayer:
                    theCrab = Crab("rsc/enemy/crab.png", 
                              [random.randint(-7,7), random.randint(-7,7)], 
                              [bsize, bsize], 
                              [random.randint(75, width-75), random.randint(75, height-75)])
                    if not theCrab.collidePlayer(player):
                        hittingPlayer = False
                crabs += [theCrab]
       
        screen.blit(bgImage, bgRect)
        for bullet in bullets:
            screen.blit(bullet.image, bullet.rect)
        screen.blit(player.image, player.rect)
        for crab in crabs:
            screen.blit(crab.image, crab.rect)
        pygame.display.flip()
        clock.tick(60)
                
    while mode == "game" and not player.living:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    mode = "start" 
                    player.living = True
        
        screen.blit(bgImage, bgRect)
        pygame.display.flip()
        clock.tick(60)









